<?php
/**
 * @auther:周卫星
 * @className:TableController.php
 * @projectName:train
 * @classDesc:
 * @date:2019/10/31
 * @time:16:01
 */

namespace App\Http\Controllers\biz;


use App\Comment\Comment;
use App\Http\Controllers\Controller;
use App\Model\BizTable;
use App\Model\BizTableRegion;
use Illuminate\Http\Request;
use SimpleSoftwareIO\QrCode\Facades\QrCode;

class TableController extends Controller
{
    public $table_region;
    public $table;
    public function __construct()
    {
        $this -> table = new BizTable();
        $this -> table_region = new BizTableRegion();
    }

    /**
     * 获取餐台列表
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index(Request $request)
    {
        if ($request -> isMethod('get')){
            // 获取餐台区域列表
            $region_list = $this -> table_region -> getTableRegionListByBiz();
            // 获取对应的餐台区域的餐台列表
            $tale_list = $this -> table -> getTableListByBiz();
            // 分类获取相对应的餐台区域的餐台列表
            $table_region_list = [];
            foreach ($region_list as $key => $value){
                foreach ($tale_list as $k => $v){
                    if ($value['id'] === $v['region_id']){
                        $table_region_list[$value['id']][] = $tale_list[$k];
                    }
                }
            }
            return view("biz.table.index", ['region' => $region_list, 'table' => $tale_list, 'table_region' => $table_region_list]);
        }
    }

    /**
     * 获取餐桌区域列表
     * @param Request $request
     * @return TableController|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function tableIndex(Request $request)
    {
        if ($request -> isMethod('get') && !$request -> ajax()){
            return view("biz.table.region_list");
        }
        if ($request -> isMethod('get') && $request -> ajax()){
            $list = $this -> table_region -> getTableRegionListByBiz();
            if ($list){
                foreach ($list as $key => $value){
                    $list[$key]['create_time'] = date("Y-m-d H:i:s", $value['create_time']);
                    $list[$key]['update_time'] = date("Y-m-d H:i:s", $value['update_time']);
                }
                $result = [
                    'code' => 0,
                    'msg' => '获取成功',
                    'data' => $list
                ];
            }else{
                $result = [
                    'code' => 1,
                    'msg' => '获取失败',
                    'data' => []
                ];
            }
            return $this -> returnJson($result);
        }
    }

    /**
     * 修改餐台区域信息
     * @param Request $request
     * @return TableController
     */
    public function updateTableRegion(Request $request)
    {
        if ($request -> isMethod('post')){
            $region = $request -> post();
            unset($region['_token']);
            $id = $region['id'];
            unset($region['id']);
            $region['update_time'] = time();
            if($this -> table_region -> updateTableRegion($id, $region)){
                $result = [
                    'code' => 0,
                    'msg' => '修改成功',
                    'data' => []
                ];
            }else{
                $result = [
                    'code' => 1,
                    'msg' => '修改失败',
                    'data' => []
                ];
            }
            return $this -> returnJson($result);
        }
    }

    /**
     * 获取餐台区域的信息
     * @param Request $request
     * @return TableController
     */
    public function tableRegionInfo(Request $request)
    {
        if ($request -> isMethod('get')){
            $id = $request -> get('id');
            $info = $this -> table_region -> getTableRegionInfo($id);
            if ($info){
                $info['token'] = csrf_token();
                $result = [
                    'code' => 0,
                    'msg' => '获取成功',
                    'data' => $info
                ];
            }else{
                $result = [
                    'code' => 1,
                    'msg' => '获取失败',
                    'data' => []
                ];
            }
            return $this -> returnJson($result);
        }
    }

    /**
     * 删除餐台区域信息以及相对应的餐台信息
     * @param Request $request
     * @return TableController
     */
    public function delTableRegion(Request $request)
    {
        if ($request -> isMethod('get')){
            $id = $request -> get('id');
            if($this -> table_region -> delTableRegion($id)){
                $result = [
                    'code' => 0,
                    'msg' => '删除成功',
                    'data' => []
                ];
            }else{
                $result = [
                    'code' => 1,
                    'msg' => '删除失败',
                    'data' => []
                ];
            }
            return $this -> returnJson($result);
        }
    }

    /**
     * 添加餐台区域信息
     * @param Request $request
     * @return TableController
     */
    public function addTableRegion(Request $request)
    {
        if ($request -> isMethod('post')){
            $data = $request -> post();
            unset($data['_token']);
            $data['create_time'] = time();
            $data['update_time'] = time();
            $data['pid'] = 0;
            $data['biz_id'] = Comment::getBizId();
            if($this -> table_region -> addTableRegion($data)){
                $result = [
                    'code' => 0,
                    'msg' => '添加成功',
                    'data' => []
                ];
            }else{
                $result = [
                    'code' => 1,
                    'msg' => '添加失败',
                    'data' => []
                ];
            }
            return $this -> returnJson($result);
        }
    }

    /**
     * 添加餐台信息
     * @param Request $request
     * @return TableController
     */
    public function addTable(Request $request)
    {
        if ($request -> isMethod('post')){
            // 获取值
            $data = $request -> post();
            $data['biz_id'] = Comment::getBizId();
            unset($data['_token']);
            if ($this -> table -> addTable($data)){
                $result = [
                    'code' => 0,
                    'msg' => '添加成功',
                    'data' => []
                ];
            }else{
                $result = [
                    'code' => 1,
                    'msg' => '添加失败',
                    'data' => []
                ];
            }
            return $this -> returnJson($result);
        }
    }

    /**
     * 删除餐台信息
     * @param Request $request
     * @return TableController
     */
    public function delTable(Request $request)
    {
        if ($request -> isMethod('get')){
            $id = $request -> get('id');
            if ($this -> table -> delTable($id)){
                $result = [
                    'code' => 0,
                    'msg' => '删除成功',
                    'data' => []
                ];
            }else{
                $result = [
                    'code' => 1,
                    'msg' => '删除失败',
                    'data' => []
                ];
            }
            return $this -> returnJson($result);
        }
    }

    /**
     * 修改餐台信息
     * @param Request $request
     * @return TableController
     */
    public function editTable(Request $request)
    {
        if ($request -> isMethod('post')){
            $data = $request -> post();
            $id = $data['id'];
            unset($data['id']);
            unset($data['_token']);
            if ($this -> table -> updateTable($id, $data)){
                $result = [
                    'code' => 0,
                    'msg' => '修改成功',
                    'data' => []
                ];
            }else{
                $result = [
                    'code' => 1,
                    'msg' => '修改失败',
                    'data' => []
                ];
            }
            return $this -> returnJson($result);
        }
    }

    /**
     * 获取餐台详情
     * @param Request $request
     * @return TableController
     */
    public function tableInfo(Request $request)
    {
        if ($request -> isMethod('get')){
            $id = $request -> get('id');
            $info = $this -> table -> getTableInfoById($id);
            if ($info){
                $result = [
                    'code' => 0,
                    'msg' => '获取成功',
                    'data' => $info
                ];
            }else{
                $result = [
                    'code' => 1,
                    'msg' => '获取失败',
                    'data' => []
                ];
            }
            return $this -> returnJson($result);
        }
    }
}
